﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using HeartLess.LevelData;

namespace HeartLess.Core
{
    public class Animation
    {        
        private const int MAX_FRAMES_PER_ROW = 20;

        public Texture2D AnimationTexture
        {
            get;
            set;
        }

        public Vector2 Location
        {
            get;
            set;
        }

        public bool RepeatEnabled
        {
            get;
            set;
        }

        public int FrameCount
        {
            get;
            set;
        }

        public int FrameTimeLimit
        {
            get;
            set;
        }

        public bool IsPaused
        {
            get;
            set;
        }

        public Color Tint
        {
            get;
            set;
        }

        private SpriteEffects _spriteEffects;
        public bool IsFlippedHorizonally
        {
            set
            {
                _spriteEffects = (value) ? SpriteEffects.FlipHorizontally
                                         : SpriteEffects.None;
            }
        }

        public AnimationCompletedAction OnAnimationCompleted
        {
            get;
            set;
        }

        private int _currentFrameIndex;
        public int CurrentFrameIndex
        {
            get { return _currentFrameIndex;}
            private set
            {
                _currentFrameIndex = value;

                int col = _currentFrameIndex % MAX_FRAMES_PER_ROW;
                int row = _currentFrameIndex / MAX_FRAMES_PER_ROW;

                int size = Level.UNIT_PIXEL_SIZE;
                int frameLocX = col * size;
                int frameLocY = row * size;

                SourceRectangle = new Rectangle(frameLocX, frameLocY, size, size);
                //CurrentFrameTime = 0;
            }
        }

        private Rectangle SourceRectangle
        {
            set;
            get;
        }

        private int CurrentFrameTime
        {
            set;
            get;
        }

        public Animation(Texture2D texture, int frameCount, int frameTime)
        {
            AnimationTexture = texture;
            FrameCount = frameCount;
            FrameTimeLimit = frameTime;

            RepeatEnabled = true;

            CurrentFrameIndex = 0;
            Tint = Color.White;
        }

        public void Update(GameTime gameTime)
        {
            CurrentFrameTime += gameTime.ElapsedGameTime.Milliseconds;

            while (CurrentFrameTime >= FrameTimeLimit)
            {
                CurrentFrameTime -= FrameTimeLimit;
                if (CurrentFrameIndex < FrameCount - 1)
                {
                    CurrentFrameIndex++;
                }
                else
                {
                    if (RepeatEnabled)
                    {
                        CurrentFrameIndex = 0;
                    }
                    else if (OnAnimationCompleted != null)
                    {
                        OnAnimationCompleted();
                    }
                }
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            Screen.Draw(spriteBatch, Location, AnimationTexture, SourceRectangle, Tint, _spriteEffects);
        }

        public delegate void AnimationCompletedAction();
    }
}
